// pages/customer/my.js
const app = getApp();
const util = require("../../utils/util.js");
const network = require("../../utils/network");

Page({
    onLoad() {
        this.getIndustryList()
        this.initUserArr()
    },
    /**
     * 页面的初始数据
     */
    data: {
        searchForm: {
            name: '',
            industry_id: '',
            industry_name: ''
        },
        userList: [],
        selectUserLoading: true,
        fabs: [{
            iconPath: 'https://oa-meilai.obs.cn-jssz1.ctyun.cn/20221102145321081195.png',
            text: '搜索'
        }, {
            iconPath: 'https://oa-meilai.obs.cn-jssz1.ctyun.cn/20221102145517219958.png',
            text: '刷新'
        }],
        popShow: false,
        customer_id: -1,
        industryPopShow: false,
        industry_arr: [],
        curTabListData: [[], []],
        tabs: [
            {
                id: 1,
                name: '公司',
                request: {
                    url: app.api.customerMy,
                    params: {
                        type: 1,
                        is_my_customer_page: false
                    },
                    searchParams: {
                        name: '',
                        industry_id: '',
                        industry_name: ''
                    }
                }
            },
            {
                id: 2,
                name: '个人',
                request: {
                    url: app.api.customerMy,
                    params: {
                        type: 2,
                        is_my_customer_page: false
                    },
                    searchParams: {
                        name: '',
                        industry_id: '',
                        industry_name: ''
                    }
                }
            },
        ]
    },

    updateTabListData({detail: {index, list: {id, name, list, listParams}}}) {
        const key = 'curTabListData[' + index + ']'
        this.setData({
            [key]: list
        }, function () {
            util.hideLoading()
        })
    },

    toPage_left1({detail: {id}}) {
        wx.navigateTo({
            url: '/pages/customer/follow?id=' + id
        });
    },

    toPage_left2({detail: {id}}) {
        wx.navigateTo({
            url: '/pages/customer/detail?id=' + id
        });
    },

    toPage_right1({detail: {id}}) {
        this.setData({
            customer_id: id,
            popShow: true
        })
    },

    toPage_right2({detail: {id}}) {
        wx.navigateTo({
            url: '/pages/customer/detail?id=' + id
        });
    },

    onConfirmDel({detail: {id}}) {
        console.log('del', id);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.selectComponent('.tabListComponent').handlePullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.selectComponent('.tabListComponent').handleReachBottom()
    },
    search() {
        const key = 'tabs[' + this.selectComponent('.tabListComponent').data.activeTabIdx + '].request.searchParams'
        this.setData({
            [key]: this.data.searchForm
        })
        this.selectComponent('.tabListComponent').handlePullDownRefresh()
    },
    reset() {
        this.setData({
            searchForm: {
                name: '',
                industry_id: '',
                industry_name: ''
            }
        })
        const key = 'tabs[' + this.selectComponent('.tabListComponent').data.activeTabIdx + '].request.searchParams'
        this.setData({
            [key]: this.data.searchForm
        })
        this.selectComponent('.tabListComponent').handlePullDownRefresh()
    },
    showSearchBox() {
        this.setData({
            searchForm: this.data.tabs[this.selectComponent('.tabListComponent').data.activeTabIdx].request.searchParams
        })
    },
    inputChange(e) {
        this.setData({
            [e.target.dataset.key]: e.detail
        })
    },
    onConfirmIndustry(e) {
        this.setData({
            'searchForm.industry_id': e.detail.value.id,
            'searchForm.industry_name': e.detail.value.name,
            industryPopShow: false
        });
    },
    onCancelIndustry() {
        this.setData({
            industryPopShow: false
        });
    },
    getIndustryList() {
        util.showLoading('加载中')
        const that = this
        network.GET({
            uri: app.api.industryList,
            success(res) {
                that.setData({
                    industry_arr: res.data
                })
                util.hideLoading()
            },
            fail() {
                util.hideLoading()
            }
        })
    },
    // 显示行业弹出层
    showIndustryPop() {
        this.setData({
            industryPopShow: true
        })
    },
    clickFab({
                 detail
             }) {
        const _this = this
        if (detail === '刷新') {
            _this.selectComponent('.tabListComponent').handlePullDownRefresh()
        }
        if (detail === '搜索') {
            _this.selectComponent('.search-box').show();
        }
    },
    onConfirm(e) {
        util.showLoading('操作中')
        network.reqPromise('POST', {
            uri: app.api.assign + '?id=' + this.data.customer_id + '&maintenance_staff_id=' + e.detail.value.id,
            params: {}
        }).then(res => {
            this.setData({
                customer_id: -1,
                popShow: false
            })
            this.selectComponent('.tabListComponent').handlePullDownRefresh()
            util.hideLoading()
        }).catch(() => {
            util.showToast('操作失败')
            util.hideLoading()
        })
    },
    onCancel() {
        this.setData({
            customer_id: -1,
            popShow: false
        })
    },
    // 初始化部门下的人员信息
    initUserArr() {
        network.reqPromise('GET', {
            uri: app.api.getSubordinateUser,
            params: {}
        }).then(res => {
            this.setData({
                userList: res.data,
                selectUserLoading: false
            })
        }).catch(() => {

        })
    }
})
